#--*--coding=utf-8--*--
#MaterialCodeGenerator2.py
import sys
import json
import sqlite3
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel, QComboBox, QMessageBox, QHBoxLayout
from PySide6.QtCore import Qt
from PySide6.QtGui import QClipboard

import chardet
"""
        # 从JSON文件加载编码规则
        with open('rules.json', 'rb') as f:
            raw_data = f.read()
            result = chardet.detect(raw_data)
            self.encoding = result['encoding']
            
        with open('rules.json', 'r', encoding=self.encoding) as f:
            self.rules = json.load(f)

"""
# 加载规则
def load_rules_from_json(file_path):
        # 从JSON文件加载编码规则
    with open(file_path, 'r') as f:
        raw_data = f.read()
        result = chardet.detect(raw_data)
        self.encoding = result['encoding']
        
    with open('rules.json', 'r', encoding=self.encoding) as f:
        self.rules = json.load(f)
    
    #with open(file_path, 'r') as file:
        #return json.load(file)

def load_rules_from_db(db_path):
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    rules = {}
    
    # 读取第1组规则
    cursor.execute("SELECT name, code FROM part1")
    rules['part1'] = [{'name': row[0], 'code': row[1]} for row in cursor.fetchall()]
    
    # 读取第2组规则
    cursor.execute("SELECT name, code FROM part2")
    rules['part2'] = [{'name': row[0], 'code': row[1]} for row in cursor.fetchall()]
    
    # 读取第3组规则
    cursor.execute("SELECT name, code FROM part3")
    rules['part3'] = [{'name': row[0], 'code': row[1]} for row in cursor.fetchall()]
    
    conn.close()
    return rules

class MaterialCodeGenerator(QWidget):
    def __init__(self):
        super().__init__()
        
        self.initUI()
        self.load_rules()

    def initUI(self):
        layout = QVBoxLayout()

        # 创建并布局第1组的选择框
        self.label_part1 = QLabel('选择第1组类别:', self)
        self.combo_part1 = QComboBox(self)
        self.combo_part1.currentIndexChanged.connect(self.update_code)
        layout.addWidget(self.label_part1)
        layout.addWidget(self.combo_part1)

        # 创建并布局第2组的选择框
        self.label_part2 = QLabel('选择第2组类别:', self)
        self.combo_part2 = QComboBox(self)
        self.combo_part2.currentIndexChanged.connect(self.update_code)
        layout.addWidget(self.label_part2)
        layout.addWidget(self.combo_part2)

        # 创建并布局第3组的选择框
        self.label_part3 = QLabel('选择第3组类别:', self)
        self.combo_part3 = QComboBox(self)
        self.combo_part3.currentIndexChanged.connect(self.update_code)
        layout.addWidget(self.label_part3)
        layout.addWidget(self.combo_part3)

        # 显示生成的编码
        self.code_label = QLabel('生成的编码: ', self)
        layout.addWidget(self.code_label)

        # 复制按钮
        self.copy_button = QPushButton('复制编码', self)
        self.copy_button.clicked.connect(self.copy_code_to_clipboard)
        layout.addWidget(self.copy_button)

        self.setLayout(layout)
        self.setWindowTitle('物料编码生成器')
        self.setGeometry(100, 100, 400, 200)

    def load_rules(self):
        # 从JSON文件加载规则
        self.rules = load_rules_from_json('rules.json')

        # 从SQLite数据库加载规则
        db_rules = load_rules_from_db('material.db')
        self.rules.update(db_rules)

        # 填充下拉列表
        self.fill_combobox(self.combo_part1, self.rules['part1'])
        self.fill_combobox(self.combo_part2, self.rules['part2'])
        self.fill_combobox(self.combo_part3, self.rules['part3'])

    def fill_combobox(self, combobox, items):
        combobox.clear()
        for item in items:
            combobox.addItem(item['name'], item['code'])

    def update_code(self):
        part1_code = self.combo_part1.currentData()
        part2_code = self.combo_part2.currentData()
        part3_code = self.combo_part3.currentData()

        if part1_code and part2_code and part3_code:
            self.generated_code = f"{part1_code}.{part2_code}.{part3_code}"
            self.code_label.setText(f'生成的编码: {self.generated_code}')
        else:
            self.code_label.setText('生成的编码: ')

    def copy_code_to_clipboard(self):
        clipboard = QApplication.clipboard()
        clipboard.setText(self.generated_code)
        QMessageBox.information(self, '提示', '编码已复制到剪贴板！')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = MaterialCodeGenerator()
    ex.show()
    sys.exit(app.exec())